package ProcessingAudioWindows;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import ControllerApplication.AudioProcessingMethods;
import ModelApplication.Filter;
import javax.swing.JSpinner;
import java.awt.FlowLayout;
import javax.swing.SpinnerNumberModel;
import javax.swing.JCheckBox;

public class W1AudioProcessingWindow extends AudioProcessingWindow {
    private JTextField textField;
    private JTable table;
    JRadioButton quarterRadioButton, halfRadioButton, noneRadioButton;
    JComboBox comboBox;
    JSpinner spinner;
    JCheckBox chckbxWartociZTabeli;

    /**
     * Create the application.
     * @wbp.parser.constructor
     */
    public W1AudioProcessingWindow() {
        super();
        initialize();
    }

    public W1AudioProcessingWindow(int x, int y) {
        super(x, y);
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {   
        frame = new JFrame();
        frame.setBounds( 100, 100, 400, 400 );

        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.NORTH);
        panel_1.setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        panel_1.add(panel, BorderLayout.NORTH);

        JLabel lblNakadanieOkna = new JLabel("Nakładanie okna");
        panel.add(lblNakadanieOkna);


        noneRadioButton = new JRadioButton("brak");
        panel.add(noneRadioButton);

        halfRadioButton = new JRadioButton("1/2");
        halfRadioButton.setSelected(true);
        panel.add(halfRadioButton);

        quarterRadioButton = new JRadioButton("3/4");
        panel.add(quarterRadioButton);

        ButtonGroup bg = new ButtonGroup();
        bg.add( noneRadioButton );
        bg.add( halfRadioButton );
        bg.add( quarterRadioButton );

        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);

        JLabel lblSzerokoOkna = new JLabel("Szerokość okna: ");
        panel_2.add(lblSzerokoOkna);

        textField = new JTextField();
        textField.setText("4096");
        panel_2.add(textField);
        textField.setColumns(10);

        JPanel panel_5 = new JPanel();
        panel_1.add(panel_5, BorderLayout.SOUTH);


        List<String> fileList = new ArrayList<String>();
        File folder = new File(System.getProperty("user.dir") + "/soundExamples/carrier/");
        for (final File fileEntry : folder.listFiles()) {
            fileList.add( fileEntry.getName() );
        }
        comboBox = new JComboBox(fileList.toArray());
        panel_5.add(comboBox);

        JPanel panel_3 = new JPanel();
        frame.getContentPane().add(panel_3, BorderLayout.CENTER);
        panel_3.setLayout(new BorderLayout(0, 0));
        
        JPanel panel_6 = new JPanel();
        panel_3.add(panel_6, BorderLayout.NORTH);
        panel_6.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JLabel lblFiltry = new JLabel("Filtry pasmowoprzepustowe");
        panel_6.add(lblFiltry);
        
        spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(7, 1, 200, 1));
        panel_6.add(spinner);
        
        chckbxWartociZTabeli = new JCheckBox("Wartości z tabeli");
        panel_6.add(chckbxWartociZTabeli);

        table = new JTable();
        table.setModel(new DefaultTableModel(
                new Object[][] {
                        {"86", "172"},
                        {"172", "345"},
                        {"345", "700"},
                        {"700", "1400"},
                        {"1400", "2800"},
                        {"2800", "5500"},
                        {"5500", "20000"},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                        {null, null},
                },
                new String[] {
                        "Zakres dolny", "Zakres g\u00F3rny"
                }
                ));
        panel_3.add(new JScrollPane(table), BorderLayout.WEST);
        //.add(table, BorderLayout.WEST);

        JButton btnPrzywr = new JButton("Przywróć");
        btnPrzywr.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                table.setModel(new DefaultTableModel(
                        new Object[][] {
                                {"86", "172"},
                                {"172", "345"},
                                {"345", "700"},
                                {"700", "1400"},
                                {"1400", "2800"},
                                {"2800", "5500"},
                                {"5500", "20000"},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                                {null, null},
                        },
                        new String[] {
                                "Zakres dolny", "Zakres g\u00F3rny"
                        }
                        ));

                textField.setText("4096");
                halfRadioButton.setSelected(true);

            }
        });

        JScrollPane scrollPane = new JScrollPane();
        panel_3.add(scrollPane, BorderLayout.EAST);
        panel_3.add(btnPrzywr, BorderLayout.SOUTH);

        JPanel panel_4 = new JPanel();
        frame.getContentPane().add(panel_4, BorderLayout.SOUTH);
        panel_4.setLayout(new BorderLayout(0, 0));

        JButton btnOk = new JButton("Ok");
        panel_4.add(btnOk);
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.setVisible( false );
            }
        });

        frame.setLocation( x, y );
    }

    public int getWindowWidth() {
        return Integer.parseInt( textField.getText() );
    }

    public int getWindowOverlap() {
        if(noneRadioButton.isSelected()) {
            return 0;
        } else if(halfRadioButton.isSelected()) {
            return 1;
        }
        return 2;
    }

    public List<Filter> getFilters() {
        int i = (int)spinner.getValue();
//        for(i = 0; i < table.getRowCount(); i++) {
//            if(table.getModel().getValueAt( i, 0 ) == null || table.getModel().getValueAt( i, 0 ).toString().isEmpty()) {
//                break;
//            }
//        }

        List<Filter> filters = new ArrayList<Filter>();
        for(int w = 0; w < i; w++) {
            filters.add(new Filter(Integer.parseInt( table.getModel().getValueAt( w, 0 ).toString() ), Integer.parseInt( table.getModel().getValueAt( w, 1 ).toString() )));
        }

        return filters;
    }
    
    public String getCarrierName() {
        return comboBox.getSelectedItem().toString();
    }
    
    public boolean isGetFilters() {
        return chckbxWartociZTabeli.isSelected();
    }

    public int getFilterCounts() {
        return (int)spinner.getValue();
    }

}
